package org.languageguide.media;


public class FlashMediaGenerator implements MediaGenerator{
	
	public final static String mediaLocationPlaceHolder = "<div id=\"placeholder1\"><a href=\"http://www.adobe.com/go/getflashplayer\">Get flash</a> to see this player	</div>";
	public static final String irJavascript = "<script language=\"JavaScript\" src=\"/applets/irflash.js\"></script>\n";
	public static final String ir2Javascript = "<script language=\"JavaScript\" src=\"/applets/ir2flash.js\"></script>\n";
	public static final String ipJavascript = "<script language=\"JavaScript\" src=\"/applets/ipflash.js\"></script>\n";
	public static final String ip2Javascript = "<script language=\"JavaScript\" src=\"/applets/ip2flash.js\"></script>\n";
	public static final String alphaJavascript = "<script language=\"JavaScript\" src=\"/applets/alphaflash.js\"></script>\n";
	public static final String alpha2Javascript = "<script language=\"JavaScript\" src=\"/applets/alpha2flash.js\"></script>\n";
	
	/*public static String[] generate(String language, String name, boolean premium){
		String[] media = new String[2];
		return media;
	}*/
	
	public static String[] get(String soundFile, Config config){
		soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.root + soundFile;
		String[] media = new String[2];
		media[0] = ipJavascript + getBasicFlashTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	
	@Deprecated
	public String[] getIrTags(String soundFile){
		soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.root + soundFile;
		String[] media = new String[2];
		media[0] = irJavascript + getFlashReaderTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	public String[] getAlpha(String soundFile, int seconds){
		soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.root + soundFile;
		String[] media = new String[2];
		media[0] = alphaJavascript + getFlashNonAutostartTag(soundFile, seconds);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	public String[] getAlpha2(String soundFile, int seconds){
		//soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.root + soundFile + ".m4a";
		String[] media = new String[2] ;
		media[0] = alpha2Javascript + getFlashNonAutostartTag(soundFile, seconds);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	  public static String getFlashReaderTag(String soundFile){
		  String flashTag =   "<script type=\"text/javascript\" src=\"/applets/swfobject.js\"></script>\r\n" +
		  "<script type=\"text/javascript\">\r\n" +
		  "  var player = null;\r\n" +
		  "	 function playerReady(thePlayer) {\r\n" +
		  "	  	player = window.document[thePlayer.id];\r\n" +
		  "	  	player.addModelListener(\"STATE\",\"myFunction\");\r\n" +
		  "	  	player.addModelListener(\"TIME\",\"time\");\r\n" +
		  "	  	check();\r\n" +
		  "	 }\r\n" +
		  " \r\n"+
		  "	 function myFunction(thePlayer){\r\n" +
		  "	  	if 	(!loaded)\r\n" +
		  "	  		stopAt(times[0] + .3)\r\n" +
		  "	  	loaded = true;\r\n" +
		  "	  }\r\n" +
		  "	  var position = null;" +
		  "	  function time(obj) {\r\n" + 
		  "	      position = obj.position;\r\n" + 
		  "	  };\r\n" +
		  "	 \r\n" +
		  "	  function set() {\r\n" +
		  "	  	var flashvars = {file:\"" + soundFile + "\", autostart:\"true\", id:\"player1\"}\r\n" + 
		  "	  	var params = {allowfullscreen:\"false\", allowscriptaccess:\"always\"}\r\n" +
		  "	  	var attributes = {id:\"player1\", name:\"player1\"};\r\n" +
		  "	  	swfobject.embedSWF(\"/applets/player.swf\", \"placeholder1\", \"180\", \"20\", \"9.0.115\", false, flashvars, params, attributes);\r\n" +
		  "	  }\r\n" +
		  "	 </script>\r\n";
		  return flashTag;
	  }
	
	
	private static String getBasicFlashTag(String soundFile){
		String flashTag = "<script type=\"text/javascript\" src=\"/applets/swfobject.js\"></script>\n" +
		"<script type=\"text/javascript\">\n" +
		"  var player = null;\n" +
		"	 function playerReady(thePlayer) {\n" +
		"	  	player = window.document[thePlayer.id];\n" +
		//"     player = getElementById(thePlayer.id);\n" +
		"	  	player.addModelListener(\"STATE\",\"myFunction\");\n" +
		"	 }\n" +
		" \n"+
		"	 function myFunction(obj){\n" +
		"	 	if (obj.newstate == 'PLAYING'){\n" +
		"	  		if 	(!loaded){\n" +
		"	  			stopAt(times[0] - .2)\n" +
		"	  			loaded = true;\n" +
		"	  			player.removeModelListener(\"STATE\",\"myFunction\");\n" +
		"	  		}\n" +
		"	  	}\n" +	  
		"	  }\n" +
		"	 \n" +
		"	  function set() {\n" +
		"	  	var flashvars = {file:\"" + soundFile + "\", autostart:\"true\", id:\"player1\"}\n" +
		"	  	var params = {allowfullscreen:\"false\", allowscriptaccess:\"always\"}\n" +
		"	  	var attributes = {id:\"player1\", name:\"player1\"};\n" +
		"	  	swfobject.embedSWF(\"/applets/player.swf\", \"placeholder1\", \"180\", \"20\", \"9.0.115\", false, flashvars, params, attributes);\n" +
		"	  }\n" +
		"	 </script>\n";
		return flashTag;
	}


	private static String getFlashNonAutostartTag(String soundFile, int seconds){
		  
		String flashTag = "<script type=\"text/javascript\" src=\"/applets/swfobject.js\"></script>\n" +
		"<script type=\"text/javascript\">\n" +
		"  var player = null;\n" +
		"	 function playerReady(thePlayer) {\n" +
		"	  	loaded = false;\n" +	  
		"	  	player = window.document[thePlayer.id];\n" +
		//"     player = getElementById(thePlayer.id);\n" +
		"	  	player.addModelListener(\"STATE\",\"myFunction\");\n" +
		"	 }\n" +
		" \n"+
		"	 function myFunction(obj){\n" +
		"	 	if (obj.newstate == 'PLAYING'){\n" +
		"	  		if 	(!loaded){\n" +
		"	  			loaded = true;\n" +	  
		"	  			randomGenerator();\n" +
		"	  			player.removeModelListener(\"STATE\",\"myFunction\");\n" +
		"	  		}\n" +
		"	  	}\n" +	  
		"	  }\n" +
		"	 \n" +
		"	  function set() {\n" +
		"	  	var flashvars = {file:\"" + soundFile + "\", autostart:\"true\", id:\"player1\", bufferlength:\"" + seconds + "\"}\n" +
		"	  	var params = {allowfullscreen:\"false\", allowscriptaccess:\"always\"}\n" +
		"	  	var attributes = {id:\"player1\", name:\"player1\"};\n" +
		"	  	swfobject.embedSWF(\"/applets/player.swf\", \"placeholder1\", \"180\", \"20\", \"9.0.115\", false, flashvars, params, attributes);\n" +
		"	  }\n" +
		"	 </script>\n";
		return flashTag;	
	}

	/*private static String getFlashNonAutostartTag(String soundFile){
		  
		String flashTag = "<script type=\"text/javascript\" src=\"/applets/swfobject.js\"></script>\n" +
		"<script type=\"text/javascript\">\n" +
		"  var player = null;\n" +
		"	 function playerReady(thePlayer) {\n" +
		"	  	loaded = false;\n" +	  
		"	  	player = window.document[thePlayer.id];\n" +
		//"     player = getElementById(thePlayer.id);\n" +
		"	  	player.addModelListener(\"STATE\",\"myFunction\");\n" +
		"	  	player.addModelListener(\"TIME\",\"time\");\r\n" +
		"	 }\n" +
		" \n"+
		  "	  var position = null;\n" +
		  "	  function time(obj) {\r\n" + 
		  "	      position = obj.position;\r\n" + 
		  "	  };\r\n" +
		  "	 \r\n" +
		"	 function myFunction(obj){\n" +
		"	 	if (obj.newstate == 'PLAYING'){\n" +
		"	  		if 	(!loaded){\n" +
		"	  			loaded = true;\n" +	  
		"	  			randomGenerator();\n" +
		"	  			player.removeModelListener(\"STATE\",\"myFunction\");\n" +
		"	  		}\n" +
		"	  	}\n" +	  
		"	  }\n" +
		"	 \n" +
		"	  function set() {\n" +
		"	  	var flashvars = {file:\"" + soundFile + "\", autostart:\"false\", id:\"player1\"}\n" +
		"	  	var params = {allowfullscreen:\"false\", allowscriptaccess:\"always\"}\n" +
		"	  	var attributes = {id:\"player1\", name:\"player1\"};\n" +
		"	  	swfobject.embedSWF(\"/applets/player.swf\", \"placeholder1\", \"180\", \"20\", \"9.0.115\", false, flashvars, params, attributes);\n" +
		"	  }\n" +
		"	 </script>\n";
		return flashTag;	
	}*/

	public String[] getIpTags(String soundFile) {
		soundFile = Utility.convertwma(soundFile);
		soundFile = Utility.root + soundFile;
		String[] media = new String[2];
		media[0] = ipJavascript + getBasicFlashTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	public String[] getIp(String soundFile) {
		soundFile = Utility.root + soundFile  + ".m4a";
		String[] media = new String[2];
		media[0] = ipJavascript + getBasicFlashTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}

	public String[] getIp2(String soundFile) {
		soundFile = Utility.root + soundFile  + ".m4a";
		String[] media = new String[2];
		media[0] = ip2Javascript + getBasicFlashTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}

	@Override
	public String getKey() {
		return "1";
	}

	public String[] getIr(String soundFile){
		soundFile = Utility.root + soundFile + ".m4a";
		String[] media = new String[2];
		media[0] = irJavascript + getFlashReaderTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}
	
	public String[] getIR2(String soundFile){
		soundFile = Utility.root + soundFile + ".m4a";
		String[] media = new String[2];
		media[0] = ir2Javascript + getFlashReaderTag(soundFile);
		media[1] = mediaLocationPlaceHolder;
		return media;
	}	
		
	public boolean useSvg(){
		return false;
	}

}